var rootPath = getRootPath();
var oriPhone;

$(function() {
	//滚动条置顶
//	$("body").prepend('<input type="text" id="focus" size="1">');
//    $("#focus").focus();
//    $("#focus").hide();
	var rleId = getUrlParam("rleId");
	if(rleId == '') {
		layer.alert("认证出错,请求参数有误:)");
	}
	//$("#rleId").val(rleId);
	var prevPage = getUrlParam("currentPage");
	$("#cpYear").css("cursor", "pointer");
	$("#cpRleId").css("cursor", "pointer");

	//回显
	$.ajax({
		url: rootPath + '/verify/uid/rleId/' + rleId + '/dr/1',
		type: 'GET',
		success: function(data) {
			if(data.returnCode == '0000') {
				var obj = data.returnObject;
				$("#cpYear").find("option[value='" + obj.cpYear + "']").prop("selected","selected");
				$("#cpRleId").find("option[value='" + obj.coachPlanRole.cpRleId + "']").prop("selected","selected");
				oriPhone = obj.phone;
				$("#phone").val(obj.phone);
				$("#email").val(obj.email);
				$("#company").val(obj.company);
				
				$("#sort").val(obj.coachPlanRole.cpRleId)
				if(obj.state == 2 || obj.state == "2" ){
					$("#cpRleId").prop("disabled","disabled");
				}
			}
		}
	});
	
	//上一步
	$("#prev").click(function() {
		var creType = $("#sort").val();
		parent.$("#content").prop("src", prevPage + "?rleId=coachPlan&creType=11&title=教练计划认证");
	});
	
	//申请认证
	$("#applyAuth").click(function() {
		if(!checkData()){
			return;
		}
		showLoading();
		$("#coachPlanAuthForm").ajaxSubmit({
			url : rootPath + '/verify/saveOrUpdateVerify',
			type: 'POST',
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if(data.returnCode == '0000'){
					layer.msg("操作成功",{icon:1,time:1000},function() {						
						parent.$("#content").prop("src","applyResult.html?rleId=coachPlan&title=教练计划认证");
					});
				} else {
					layer.alert(data.returnMessage);
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				hideLoading();
				layer.alert("申请失败,请联系管理员");
			}
		});
		return false;
	});
});

function checkData(){
	var year = $("#cpYear option:selected").val();
	if(year == 0) {
		$("#cpYear").focus();
		showErrMsg("cpYear","请选择年份");
		return false;
	}
	var cpRleId = $("#cpRleId option:selected").val();
	if(!cpRleId || cpRleId == '') {
		$("#cpRleId").focus();
		showErrMsg("cpRleId","请选择角色");
		return false;
	}
	if($.trim($("#phone").val()) == '') {
		$("#phone").focus();
		showErrMsg("phone", "请输入手机号码");
		return false;
	} else {
		if($("#phone").val().indexOf("*")!=-1){
			$("#phone").focus();
			showErrMsg("phone","请重新输入手机号");
			return false;
		} else if(!checkPhone($("#phone").val())) {
			$("#phone").focus();
			showErrMsg("phone","手机号码格式有误");
			return false;
		}
	}
	
	if($.trim($("#email").val()) == '') {
		$("#email").focus();
		showErrMsg("email","请输入邮箱地址");
		return false;
	} else {
		if($("#email").val().indexOf("*")!=-1){
			$("#email").focus();
			showErrMsg("email","请重新输入邮箱");
			return false;
		}else if(!checkEmail($("#email").val())) {
			$("#email").focus();
			showErrMsg("email","邮箱格式有误");
			return false;
		}
	}

	//机构,其他都未填入
	if($.trim($("#company").val()) == '') {
		$("#company").focus();
		showErrMsg("company", "请输入机构/单位");
		return false;
	}
	
	//检查其他长度
	if($.trim($("#company").val()).length > 50) {
		$("#company").focus();
		showErrMsg("company", "其他输入过长,请精简");
		return false;
	}
	return true;
}

function changeRole(e){
	$(e).removeAttr("style","color");
	//原先的角色ID 后台逻辑处理
	var sortVal = $("#cpRleId").val();
	hideMsg("cpRleId");
	$("#sort").val(sortVal);
	 
}